<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class wxapp_model extends model{
	
    public function setConfig($data = array())
    {
        $config  =  $this -> select_all('app_config',array(),'`name`');
        
        foreach($config as $v){
            
            $allList[]  =  $v['name'];
            
        }
        
        foreach($data as $key=>$v){
            
            if(in_array($key,$allList)){
                
                $this->update_once('app_config',array('config'=>$v),array('name'=>$key));
                
            }else{
                
                $this->insert_into('app_config',array('name'=>$key,'config'=>$v));
            }
        }
        $this->makeConfig();
    }
    /**
     * 生成appp配置缓存
     */
    public function makeConfig()
    {
        $config  =  $this -> select_all('app_config',array());
        
        if(is_array($config)){
            foreach($config as $v){
                $configarr[$v['name']]	=	$v['config'];
            }
        }
        if(!empty($configarr)){
            made_web(DATA_PATH.'api/wxapp/app.config.php',ArrayToString($configarr),'appconfig');
        }
    }
    /**
     * 移动端自定义设置保存
     */
    public function saveTplApp($data){
        
        if(!empty($data)){
            
            $tList	=	$this->select_all("tplapp",array('type'=>'index'),'`name`');
            
            foreach($tList as $k=>$v){
                
                $nameList[$v['name']]	=	1;
            }
        }
        foreach ($data as $key=>$val){
            
            if(isset($nameList[$key]) && $nameList[$key]==1){
                
                $this->update_once("tplapp",array('config'=>$val),array('name'=>$key));
                
            }else{
                
                $this->insert_into("tplapp",array('name'=>$key,'config'=>$val,'type'=>'index'));
            }
        }
    }
    
    /**
     *
     */
    public function sortTplApp($data){
        
        if(!empty($data)){
            
            $num		=		$this->select_num("tplapp",array('name'=>'sort'));
            
            $sort		=		implode(',', $data);
            
            if($num){
                
                $nid	=		$this->update_once("tplapp",array('config'=>$sort),array('name'=>'sort'));
                
            }else{
                
                $nid	=		$this->insert_into("tplapp",array('name'=>'sort','config'=>$sort,'type'=>'index'));
                
            }
        }
        return	$nid;
    }
    /**
     *
     */
    public function getTplAppList($whereData){
        
        $List			=	array();
        
        if($whereData){
            
            $List		=	$this -> select_all('tplapp',$whereData);
        }
        return	$List;
    }
    /**
     *
     */
    public function delTplApp($type,$post){
        
        $tpltype 		= 	$this->getTplAppList(array('type'=>$type));
        
        foreach($tpltype as $k=>$v){
            
            if(!in_array($v['id'], $post)){
                $nid		=	$this->delete_all('tplapp',array('id'=>$v['id']));
            }
        }
        return	$nid;
    }
    
    /**
     *
     */
    public function saveAllTplApp($whereData,$data){
        
        if($whereData['id']!=''){
            
            $nid		=	$this -> update_once('tplapp',$data,$whereData);
            
        }else{
            
            $nid		=	$this -> insert_into('tplapp',$data);
        }
        return	$nid;
    }
}
?>